//
//  YXReplyViewController.m
//  CreditCard
//
//  Created by 杨旭 on 2018/1/20.
//  Copyright © 2018年 杨旭. All rights reserved.
//

#import "YXReplyViewController.h"
#import "YXLeaveCell.h"
#import "YXReplyModel.h"
#import "YXReplyHeaderView.h"
#import "YXTopicFooterView.h"
@interface YXReplyViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArr;
@end

@implementation YXReplyViewController

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)) style:(UITableViewStylePlain)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 120;
        _tableView .rowHeight = UITableViewAutomaticDimension;
        
        [_tableView registerNib:[UINib nibWithNibName:@"YXLeaveCell" bundle:nil] forCellReuseIdentifier:@"YXLeaveCell"];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createLeftBarButtonItemWithImage:@"导航栏_002"];

    self.title = @"全部回复";
    [self requestData];
    [self.view addSubview:self.tableView];
}

- (void)requestData {
//http://121.196.210.134:8082/mpos/wx/replyPostInfo.do?sessionId=c517b5c1-4352-4c20-a336-5fb4ad31a24f&id=1
    NSString *urlStr = @"replyPostInfo.do";
    NSDictionary *parameter = @{@"id":self.ID};
    [BX_NetWorkManager requestForPOSTWithUrl:urlStr parameter:parameter success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        if (REQUESTDATASUCCESS) {
            self.dataArr = [YXReplyModel mj_objectArrayWithKeyValuesArray:responseObject[@"body"]];
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        [self showAlertWithTitle:@"提示" Messge:@"维护中，请稍等！"];

    }];

    
}

#pragma mark - UITableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    YXReplyModel *model = [self.dataArr objectAtIndex:section];
    return model.replyAgentContents == nil ? 0 : 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YXLeaveCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXLeaveCell" forIndexPath:indexPath];
    cell.model = self.dataArr[indexPath.section];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    YXReplyModel *model = [self.dataArr objectAtIndex:indexPath.section];
    return model.cellHeight;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YXReplyHeaderView *headerView = [[YXReplyHeaderView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 120))];
    headerView.model = self.dataArr[section];
    
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    YXReplyModel *model = [self.dataArr objectAtIndex:section];
    CGFloat contentHeight = [self getHeightWtih:model.contents WithFont:14];
    return contentHeight + 90;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *backView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth,10))];
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = color_lineColor;
    [backView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.and.right.equalTo(backView);
        make.height.mas_equalTo(1);
    }];
    return backView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    YXReplyModel *model = [self.dataArr objectAtIndex:section];
    return model.replyAgentContents == nil  ? 1 : 10;
}

#pragma maek - 根据cell上字符串的长度和label的字体大小，计算label的高度
- (CGFloat)getHeightWtih:(NSString *)contentStr WithFont:(CGFloat)font {
    CGSize size = [contentStr boundingRectWithSize:(CGSizeMake(kScreenWidth - 20, MAXFLOAT)) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:font]} context:nil].size;
    if (contentStr.length > 0) {
        return size.height - 17;
    }
    return size.height;
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 120;
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

@end
